#!/usr/bin/env python
# encoding: utf-8
#################
# Name:        更新数据总览表， 每个更新ID1 ,周一更新ID0 
# Purpose:
#
# Author:      qfh 
#
# Created:     02/26/2019
# Licence:     1.0.0
#
#################

import datetime

__add__ = ['generate_insert_sql', 'generate_update_sql' ]
#generate insert sql
def generate_insert_sql( add_dict, table):
  if type(add_dict) != dict or len(add_dict) <1:
    return

  insert_sql = "insert into %s ( " %(table)
  values_sql = "values ("
  for key, val in add_dict.items():
    insert_sql += key + ','
    if type(val) == int:
      values_sql += '%d'%(val) + ','
    elif type(val) == float:
      values_sql += '%f'%(val) + ','
    else:
      values_sql += "'%s'"%(val) +','
  insert_sql += "add_user_id, add_time" +','
  values_sql += "%d, '%s' "%(1, datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) +','
  if insert_sql[-1] == ',':
    insert_sql = insert_sql[:-1] + ') '
  if values_sql[-1]==',':
    values_sql = values_sql[:-1] + ') '

  sql = insert_sql + values_sql
  return sql

#generate sql                                                              
def generate_update_sql(add_dict, table):
  if type(add_dict) != dict or len(add_dict) <1:
    return
  if not add_dict.get('imei'):
    return 
   
  wh = ''
  update_key = ''
  if add_dict.get('imei') is not None:    
    wh += "where imei = '%s'"%(add_dict.get('imei'))                       
    update_key += 'imei'  
                                                
  if len(wh) <1:                                                           
    return 
  if (update_key)<0:                                                       
    return 
 
  update_sql = "update %s set " %(table)                          
  for key, val in add_dict.items():                                        
    if key == update_key:                                                  
      continue                                                             
    if type(val) == int:                                                   
      update_sql +=  '%s = %d'%(key, val) + ','                            
    elif type(val) == float:                                               
      update_sql += ' %s = %f'%(key, val) + ','                            
    else:                                                                  
      update_sql += "%s = '%s'"%(key, val) +','                            
  update_sql += "update_user_id = %d, update_time='%s' "%(1, \
               datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) +',' 
  if update_sql[-1]==',':                                                  
    update_sql = update_sql[:-1]                                           
  update_sql += wh                                                         
  return update_sql  

